package com.qf.dao;

import com.qf.pojo.Goods;
import com.qf.util.DataSourceUtil;
import com.qf.util.StrUtils;
import com.qf.vo.GoodsVo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 千锋健哥
 */
public class GoodsDaoImpl implements GoodsDao{

    private QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());

    @Override
    public List<GoodsVo> findGoodsList(String name, String pubDate) {
        try {
            String sql = "select a.*, b.`name` as typeName " +
                    " from  tb_goods a left join tb_goods_type b " +
                    " on a.tid=b.id where 1=1 " ;
            //判断名字不为空, 那么拼接上名字作为查询条件
            if (StrUtils.empty(name)) {
                sql = sql + " and a.name like '%" + name + "%'";
            }
            //判断如果日期不为空, 那么日期作为查询条件
            if(StrUtils.empty(pubDate)) {
                sql = sql + " and a.pubdate='" + pubDate + "'";
            }

            sql = sql + " order by id desc";
            System.out.println("sql=====" + sql);

            List<GoodsVo> list = queryRunner.query(sql, new BeanListHandler<GoodsVo>(GoodsVo.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void insert(Goods goods) {
        try {
            String sql = "insert into tb_goods (name, pubdate, picture, price, star, intro, tid) " +
                    " values(?, ?, ?, ?, ?, ?, ?)";
            queryRunner.update(sql, goods.getName(), goods.getPubdate(),
                    goods.getPicture(), goods.getPrice(), goods.getStar(),
                    goods.getIntro(), goods.getTid());

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public long findPageCount(String goodsName) {
        String sql = "select count(*)  from  tb_goods where name like '%" + goodsName + "%'";
        try {
            Long count = queryRunner.query(sql, new ScalarHandler<Long>());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Goods> findPage(String goodsName, Integer startRows, Integer pageSize) {
        String sql = "select *  from  tb_goods where name like '%" + goodsName + "%' limit ?, ?";
        try {
            List<Goods> list = queryRunner.query(sql, new BeanListHandler<Goods>(Goods.class),
                    startRows, pageSize);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Goods findById(Integer id) {
        String sql = "select *  from  tb_goods where id = ?";
        try {
            Goods goods = queryRunner.query(sql, new BeanHandler<Goods>(Goods.class), id);
            return goods;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
